//
// Created by v on 19-6-27.
//

#include "CMHttpExCurl.h"
#include "utils/cm_common.h"
#include "utils/cm_logprint.h"

CMLogPrint logPrint;

class CMHttpRecvData : public CMHttpExSetFunction
{
  public:
    size_t read_callback(char *buffer, size_t size, size_t nitems,
                         void *userdata) CM_CPP_OVERRIDE
    {
        return CMHttpExSetFunction::read_callback(buffer, size, nitems,
                                                  userdata);
    }

    size_t write_data(void *buffer, size_t size, size_t nmemb,
                      void *userp) CM_CPP_OVERRIDE
    {
        printf("[%lu]\n", size * nmemb);
        printf("[%s]\n", buffer);
        return size * nmemb;
    }
};

void net_http_test(const char *url)
{
    cminfo("url = %s\n", url);
    CMHttpEx *http = new CMHttpExCurl();
    http->setDebug(true);
    CMHttpExSetFunction *f = new CMHttpRecvData();
    http->setFunction(f);

    http->setSaveFile("/tmp/testout.html");

    int ret = http->getSecure(url, NULL, 10, 10);
    if (ret != 0)
    {
        cmerror("get with result [%d]\n", ret);
    }
    cminfo("get success\n");
    delete http;
}

int main(int argc, char **args)
{
    CM_LOG_PRINT_INIT("net");
    const char *url = args[1];
    net_http_test(url);

    getchar();
    return 0;
}
